package com.test.xingke.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.test.xingke.entity.XingKeCMD;
/***
 * 
 * 连接星科，访问星科小程序接口
 * 
 * @author ggy
 * @date 20190827
 *
 */
public class XingkeCppService {

	  /**
  	  * 调用星科的接口
  	  * @return
  	  * @throws Exception
  	  */
  	 public static Map<String,Object> xingkeCppResponse(String resUrl,Map params,String requestMethod,String openId) throws Exception {
  			// TODO Auto-generated method stub
  	        URL httpUrl = new URL(resUrl);
  	        String reqBody  =JSON.toJSONString(params);
  	        HttpURLConnection httpURLConnection = (HttpURLConnection) httpUrl.openConnection();
  	        httpURLConnection.setDoOutput(true);
  	        httpURLConnection.setUseCaches(false);//post不能使用缓存
  	        httpURLConnection.setInstanceFollowRedirects(true);
  	        httpURLConnection.setRequestProperty("accept", "*/*");
  	        httpURLConnection.setRequestProperty("connection", "Keep-Alive");
  	        httpURLConnection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
	        httpURLConnection.setRequestProperty("content-type", "application/json; charset=utf-8");
  	        if(openId !=null) {
  	        	httpURLConnection.setRequestProperty("Authorization", openId);
  	        }
  	        httpURLConnection.setRequestMethod(requestMethod);
  	        httpURLConnection.setConnectTimeout(10*1000);
  	        httpURLConnection.setReadTimeout(10*1000);
  	        httpURLConnection.connect();
  	        OutputStream outputStream = httpURLConnection.getOutputStream();
  	        outputStream.write(reqBody.getBytes(XingKeCMD.UTF8));
  	        InputStream inputStream = httpURLConnection.getInputStream();
  	        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,XingKeCMD.UTF8));
  	        final StringBuffer stringBuffer = new StringBuffer();
  	        String line = null;
  	        while ((line = bufferedReader.readLine()) != null) {
  	            stringBuffer.append(line);
  	        }
  	        String resp = stringBuffer.toString();
  	        if (stringBuffer!=null) {
  	            try {
  	                bufferedReader.close();
  	            } catch (IOException e) {
  	                e.printStackTrace();
  	            }
  	        }
  	        if (inputStream!=null) {
  	            try {
  	                inputStream.close();
  	            } catch (IOException e) {
  	                e.printStackTrace();
  	            }
  	        }
  	        if (outputStream!=null) {
  	            try {
  	                outputStream.close();
  	            } catch (IOException e) {
  	                e.printStackTrace();
  	            }
  	        }
  	        JSONObject jsStr = JSONObject.parseObject(resp);
  	        Map<String, Object> map = (Map)jsStr;
  	        return map;
  		}
  	 
  	/**
  	  * 调用星科的接口
  	  * @return
  	  * @throws Exception
  	  */
  	 public static Map<String,Object> xingkeGetCppResponse(String resUrl,Map params,String requestMethod,String openId) throws Exception {
  			// TODO Auto-generated method stub
  	        URL httpUrl = new URL(resUrl);
  	        String reqBody  =JSON.toJSONString(params);
  	        HttpURLConnection httpURLConnection = (HttpURLConnection) httpUrl.openConnection();
  	        httpURLConnection.setDoOutput(false);
  	        httpURLConnection.setUseCaches(false);//post不能使用缓存
  	        httpURLConnection.setInstanceFollowRedirects(true);
  	        httpURLConnection.setRequestProperty("accept", "*/*");
  	        httpURLConnection.setRequestProperty("connection", "Keep-Alive");
  	        httpURLConnection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
	        httpURLConnection.setRequestProperty("content-type", "application/json; charset=utf-8");
  	        if(openId !=null) {
  	        	httpURLConnection.setRequestProperty("Authorization", openId);
  	        }
  	        httpURLConnection.setRequestMethod(requestMethod);
  	        httpURLConnection.setConnectTimeout(10*1000);
  	        httpURLConnection.setReadTimeout(10*1000);
  	        httpURLConnection.connect();
  	        InputStream inputStream = httpURLConnection.getInputStream();
  	        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,XingKeCMD.UTF8));
  	        final StringBuffer stringBuffer = new StringBuffer();
  	        String line = null;
  	        while ((line = bufferedReader.readLine()) != null) {
  	            stringBuffer.append(line);
  	        }
  	        String resp = stringBuffer.toString();
  	        if (stringBuffer!=null) {
  	            try {
  	                bufferedReader.close();
  	            } catch (IOException e) {
  	                e.printStackTrace();
  	            }
  	        }
  	        if (inputStream!=null) {
  	            try {
  	                inputStream.close();
  	            } catch (IOException e) {
  	                e.printStackTrace();
  	            }
  	        }
  	        JSONObject jsStr = JSONObject.parseObject(resp);
  	        Map<String, Object> map = (Map)jsStr;
  	        return map;
  		}
}
